package com.woniuxy.springparking.controller;

import com.woniuxy.springparking.asepct.SysLog;
import com.woniuxy.springparking.common.RestDto;
import com.woniuxy.springparking.common.RestResult;
import com.woniuxy.springparking.pojo.TPerCoupon;
import com.woniuxy.springparking.service.TPerCouponService;
import com.woniuxy.springparking.vo.TPerCouponVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
public class TPerCouponController {
    @Autowired
    private TPerCouponService tPerCouponService;

    @GetMapping("/TCSelectByPage.do")
    @ApiOperation(value = "分页查询所有代金卷信息",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = RestResult.class,notes = "分页查询所有代金卷信息")
    public RestResult tcSelectByPage(TPerCouponVo tcVo){
        return tPerCouponService.findTCByPage(tcVo);
    }

    @PostMapping("/xgCoupon.do")
    @ApiOperation(value = "按id更新代金卷信息",httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = RestDto.class,notes = "按id更新代金卷信息")
    @SysLog(value1 = "更新",value2 = "更新代金券信息")
    public RestDto xgCoupon(Integer perCouponNominal, Date perCouponEndtime, Integer perCouponId){
        RestDto dto=new RestDto();
        this.tPerCouponService.updateCoupon(perCouponNominal,perCouponEndtime,perCouponId);
        dto.setCode(200);
        dto.setMsg("修改成功");
        return dto;
    }

    @DeleteMapping("/deleteCoupon.do")
    @ApiOperation(value = "按id删除代金卷",httpMethod = "DELETE",
            protocols = "HTTP",produces = "application/json",
            response = RestDto.class,notes = "按id删除代金卷")
    @SysLog(value1 = "删除",value2 = "删除代金券")
    public RestDto deleteCoupon(Integer perCouponId) {
        RestDto dto = new RestDto();
        try {
           this.tPerCouponService.remove(perCouponId);
            dto.setCode(200);
            dto.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            dto.setCode(-1);
            dto.setMsg("删除成功");
        }
        return dto;
    }

    //批量删除
    @DeleteMapping("/deleteManyCoupon.do")
    @ApiOperation(value = "批量删除代金卷",httpMethod = "DELETE",
            protocols = "HTTP",produces = "application/json",
            response = RestDto.class,notes = "批量删除代金卷")
    @SysLog(value1 = "删除",value2 = "批量删除代金券")
    public RestDto deleteManyLog(@RequestParam("perCouponIds[]") Integer[] perCouponIds) {
        RestDto dto = new RestDto();
        try {
            this.tPerCouponService.removeBech(perCouponIds);
            dto.setCode(200);
            dto.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            dto.setCode(-1);
            dto.setMsg("删除成功");
        }
        return dto;
    }

    @PostMapping("/insertCoupon.do")
    @ApiOperation(value = "增加代金卷",httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = RestDto.class,notes = "增加代金卷")
    @SysLog(value1 = "添加",value2 = "添加代金券")
    public RestDto insertCoupon(TPerCoupon tPerCoupon){
        RestDto dto=new RestDto();
        this.tPerCouponService.addAll(tPerCoupon);
        dto.setCode(200);
        dto.setMsg("添加成功");
        return dto;
    }


}
